
import java.awt.Component;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.AWTEvent;
import java.awt.event.AWTEventListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.SwingUtilities;

public class IMEDemo {
    protected JTextArea textArea;
    protected JTextField textField;
    protected JComboBox<String> comboBox;

    public IMEDemo() {
        JFrame frame = new JFrame();
        frame.setSize(800, 200);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent ev) {
                System.exit(0);
            }
        });
        // 显示在屏幕中间
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Point screenCenter = ge.getCenterPoint();
        frame.setLocation(screenCenter.x - frame.getWidth() / 2, screenCenter.y - frame.getHeight() / 2);

        textArea = new JTextArea();
        textArea.setText("JTextArea");
        textArea.setBounds(10, 10, 300, 30);
        textArea.addFocusListener(new InputFocusListener(textArea, FocusEvent.FOCUS_GAINED | FocusEvent.FOCUS_LOST));

        textField = new JTextField();
        textField.setText("JTextField");
        textField.setBounds(320, 10, 300, 30);
        textField.addFocusListener(new InputFocusListener(textField, FocusEvent.FOCUS_GAINED | FocusEvent.FOCUS_LOST));

        String[] items = new String[]{"JList", "JTable", "JTree", "JComboBox"};
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(items);
        comboBox = new JComboBox<String>();
        comboBox.setModel(model);
        comboBox.setBounds(10, 50, 300, 30);
        comboBox.addFocusListener(new InputFocusListener(comboBox, FocusEvent.FOCUS_GAINED | FocusEvent.FOCUS_LOST));

        frame.getContentPane().setLayout(null);
        frame.getContentPane().add(textArea);
        frame.getContentPane().add(textField);
        frame.getContentPane().add(comboBox);

        frame.setVisible(true);
    }

    class InputFocusListener extends FocusEvent implements FocusListener {
        public InputFocusListener(Component source, int id) {
            super(source, id);
        }

        @Override
        public void focusGained(FocusEvent ev) {
            System.out.println("focusGained" + " " + ev.getSource().getClass().toGenericString());
        }

        @Override
        public void focusLost(FocusEvent ev) {
            System.out.println("focusLost" + " " + ev.getSource().getClass().toGenericString());
        }
    }

    public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
           @Override
           public void run() {
               new IMEDemo();
           }
       });
   }
}
